package com.lyh.societiesmanager.controller;

import com.lyh.societiesmanager.ClubService.ActivityService;
import com.lyh.societiesmanager.ClubService.ClubService;
import com.lyh.societiesmanager.ClubService.MemberService;
import com.lyh.societiesmanager.entity.Activity;
import com.lyh.societiesmanager.entity.Club;
import com.lyh.societiesmanager.entity.Member;
import com.lyh.societiesmanager.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/clubs")
@Api(tags = "社团管理接口")
public class ClubController {

    @Autowired
    private ClubService clubService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ActivityService activityService;

    @ApiOperation("查询所有社团")
    @GetMapping
    public Response<List<Club>> getAllClubs() {
        List<Club> clubs = clubService.list();
        return Response.success(clubs);
    }

    @ApiOperation("根据ID查询社团")
    @GetMapping("/{id}")
    public Response<Club> getClubById(@PathVariable Integer id) {
        Club club = clubService.getById(id);
        if (club != null) {
            return Response.success(club);
        } else {
            return Response.error(404, "未找到该社团");
        }
    }

    @ApiOperation("新增社团")
    @PostMapping
    public Response<Boolean> addClub(@RequestBody Club club) {
        boolean isSaved = clubService.save(club);
        return isSaved ? Response.success("添加成功", true) : Response.error(500, "添加失败");
    }

    @ApiOperation("更新社团信息")
    @PutMapping("/{id}")
    public Response<Boolean> updateClub(@PathVariable Integer id, @RequestBody Club club) {
        club.setClubId(id);
        boolean isUpdated = clubService.updateById(club);
        return isUpdated ? Response.success("更新成功", true) : Response.error(500, "更新失败");
    }

    @ApiOperation("删除社团")
    @DeleteMapping("/{id}")
    public Response<Boolean> deleteClub(@PathVariable Integer id) {
        boolean isDeleted = clubService.removeById(id);
        return isDeleted ? Response.success("删除成功", true) : Response.error(500, "删除失败");
    }

}